package com.tianpengtech.admin.fairwithdraw;
import java.util.Map;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairauth.FairAuth;
import com.tianpengtech.common.model.fairmessage.FairMessage;
import com.tianpengtech.common.model.fairwithdraw.FairWithdraw;
import com.tianpengtech.common.model.moneylog.MoneyLog;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.model.platformincome.PlatFormIncome;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.util.push.Jpush;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeEqual;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;

public class FairWithdrawAdminServiceImpl extends AbstractAdmin implements IAdminOpt{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {

		int status = model.getInt(FairWithdraw.STATUS);
		if(status>-1){
			queryConstruct.and(new EqualCondition(FairWithdraw.STATUS, status));
		}
	}

	@Override
	public String tableName() {
		return FairWithdraw.dao.tableName();
	}

	@Override
	public void changeStatus(Model<?> model, long id, int status, Kv params) throws ValidateException {
		FairWithdraw temp = FairWithdraw.dao.findById(id);
		
		if(temp.getInt(FairWithdraw.STATUS)!=FairWithdraw.STATUS_APPROVING){
			throw new ValidateException("当前记录已经被处理过了，请勿重复当前操作");
		}
		
		String content = params.getStr(Opertion.CONTENT);
		Validator validator = new Validator();
		
		if(status==FairWithdraw.STATUS_ERROR){
			validator.addRule( new StringLengthBetween(content,"拒绝理由必须是1-40个字符之间",1,40));
		}
		
		validator.addRule( new NumbericMustBeGreaterThan(id,0,"id值必须要大于0")).excute();
		
		
		model.set(FairWithdraw.ID, id);
		model.set(FairWithdraw.STATUS, status);
		model.update();
		
		Fair fair = Fair.dao.findById(temp.getInt(FairWithdraw.WH_ID));
		Account account = XxxUtil.readSessionInfo();
		long userId = account.getLong(Account.ID);
		
		
		
		if(status==FairWithdraw.STATUS_SUCCESS ){
			String result = "【通过】你的提现申请请求";
			new FairMessage().set(FairMessage.CONTENT, "管理员"+result)
			.set(FairMessage.WH_ID, temp.getInt(FairAuth.WH_ID)).save();
			result = "【通过】"+fair.getStr(Fair.NICK_NAME)+"提现的认证请求";
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_GET_CASH,userId,result);
			String sql = "select * from "+MoneyLog.dao.tableName()+" where operid=? limit 1";
			MoneyLog moneyLog = MoneyLog.dao.findFirst(sql,id);
			double money = moneyLog.getDouble(MoneyLog.MONEY);
			double actualFee= money-temp.platFormFee();
			moneyLog.set(MoneyLog.MONEY,(int)money);
			moneyLog.set(MoneyLog.DESCRIPTION,"用户提现：金额￥"+money+",实际收益：￥"+(int)actualFee);
			moneyLog.set(MoneyLog.DELETETAG,MoneyLog.STATUS_SUCCESS).update();
			
			//更新网红余额数据信息
			double leftMomey = fair.getDouble(Fair.MONEY);
			
			if(leftMomey<0){
				throw new ValidateException("当前网红账户提现金额已经超出账户余额");
			}
			fair.set(Fair.AVALIABLE_FEE,leftMomey);
			fair.update();
			
			PlatFormIncome formIncome = new PlatFormIncome();
			formIncome.set(PlatFormIncome.WH_ID, temp.getInt(FairAuth.WH_ID));
			formIncome.set(PlatFormIncome.MONEY, temp.platFormFee());
			formIncome.set(PlatFormIncome.WITH_DRAW_MONEY, money);
			formIncome.set(PlatFormIncome.STATUS,PlatFormIncome.STATUS_OK);
			formIncome.save();
			
			Jpush.pushCustomContentToSomeOne(fair.getStr(Fair.MOIBILE), result, Jpush.PUSH_MSG);
			Jpush.pushMessageInfoToSomeOne(fair.getStr(Fair.MOIBILE), result, Jpush.PUSH_MSG);
		}
		if(status==FairWithdraw.STATUS_ERROR){
			String result = "【拒绝】你的提现申请请求,原因是:"+content;
			new FairMessage().set(FairMessage.CONTENT, "管理员"+result)
			.set(FairMessage.WH_ID, temp.getInt(FairAuth.WH_ID)).save();
			result = "【拒绝】"+fair.getStr(Fair.NICK_NAME)+"提现申请请求,原因是:"+content;
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_GET_CASH,userId,result);
			String sql = "select * from "+MoneyLog.dao.tableName()+" where operid=? limit 1";
			MoneyLog moneyLog = MoneyLog.dao.findFirst(sql,id);
			moneyLog.set(MoneyLog.DELETETAG,MoneyLog.STATUS_ERROR).update();
//			将提现的金额重新填入账户余额中
			double currentMoney = temp.getDouble(FairWithdraw.MONEY)+fair.getDouble(Fair.MONEY);
			fair.set(Fair.MONEY, currentMoney);
			fair.set(Fair.AVALIABLE_FEE, currentMoney);
			fair.update();
			
//			double money = moneyLog.getDouble(MoneyLog.MONEY);
//			//更新网红余额数据信息
//			double leftMomey = fair.getDouble(Fair.MONEY);
//			double currentMoney = leftMomey-money;
//			
//			if(currentMoney<0){
//				throw new ValidateException("当前网红账户提现金额已经超出账户余额");
//			}
//			fair.set(Fair.MONEY,currentMoney);
//			fair.set(Fair.AVALIABLE_FEE,currentMoney);
//			fair.update();
			Jpush.pushCustomContentToSomeOne(fair.getStr(Fair.MOIBILE), result, Jpush.PUSH_MSG);
			Jpush.pushMessageInfoToSomeOne(fair.getStr(Fair.MOIBILE), result, Jpush.PUSH_MSG);
		}
	}

}